//但前激活key元素
keyMap.e=null;          //document.getElementsByClassName('active')[0];
//所有key元素 
keyMap.eAll=document.getElementsByClassName('key-name');
//key元素单击添加选中事件 
Array.prototype.forEach.call(keyMap.eAll,(e,i)=>{
    e.index=i;
    e.addEventListener('click',(ev)=>{
        if(keyMap.e) keyMap.e.className=keyMap.e.className.replace(/ active/,'');
        keyMap.e=ev.target;
        keyMap.e.className+=" active";
    });
})
keyMap.copy=  function(text){
            var textarea = document.createElement("textarea");
            keyMap.e.appendChild(textarea);
            textarea.value = text;
            textarea.focus();
            if (textarea.setSelectionRange)
                textarea.setSelectionRange(0, textarea.value.length);
            else       textarea.select();
           document.execCommand("copy");
           keyMap.e.removeChild(textarea);
           console.log("copy:  "+text);
    }
let Each=Array.prototype.forEach;
//添加页面键盘事件
document.addEventListener('keydown',function(event){
  //if(event.target.tagName=='A' || event.target.tagName=='INPUT') return true;
  let aliasName=keyMap.get(event),e=keyMap.e,old_e=e;
  //模仿vim i进入编辑模式 
  if(!e && aliasName=='i')  { e=keyMap.eAll[0];  aliasName='';} 
  if(!e) return true; //e=keyMap.eAll[0];
  
  if(aliasName)
   switch(aliasName ){
       case "f1":
             window.open(chrome.runtime.getURL("readme.html#json"));
            return keyMap.stop(event);
       case "esc":
             if(keyMap.e) keyMap.e.className=keyMap.e.className.replace(/ active/,'');
            return keyMap.e=null;
       case "left":   case 'h':
           if(e.box && e.box.style.display!='none') {
                return e.previousElementSibling.click() 
           }else{
                 e=e.parentElement.key;
            }
            break;
       case "enter": 
       case "space":
       case "right":   case 'l':
           if(!e.box) return true;
           if(e.box.style.display!='none') {
               e=e.box.children[0];
               if(e.tagName=="I") e=e.nextElementSibling;
           }else{
               return e.previousElementSibling.click() 
            } 
            break;
       case "up":   case 'k':
           p= e.previousElementSibling? e.previousElementSibling: e.parentElement.previousElementSibling ;
           if(p.box) p= p.previousElementSibling? p.previousElementSibling: p.parentElement.previousElementSibling ;
           while(!(!p || p.tagName=="I" || p.className.match(/key-name/))){
               p=(p.key && !p.isClosed) ? p.lastElementChild :  (p.previousElementSibling? p.previousElementSibling: p.parentElement);
           }
             if(p) e=(p.tagName=="I") ? p.nextElementSibling :p;
            keyMap.stop(event);
            break;
       case "down":   case 'j':
           p=e.nextElementSibling? e.nextElementSibling: e.parentElement.nextElementSibling;
           while(!(!p || p.tagName=="I" || p.className.match(/key-name/))){
               p=(p.key && !p.isClosed) ? p.firstElementChild :  (p.nextElementSibling? p.nextElementSibling: p.parentElement.nextElementSibling);
           }
            if(p) e=(p.tagName=="I") ? p.nextElementSibling :p;
            keyMap.stop(event);
            break;
       case "home":  case '0':
            e=e.parentElement.firstElementChild;
            keyMap.stop(event);
            break;
       case "end":    case 'g':
                p=e.parentElement.lastElementChild;
                while(!p.className.match(/key-name/)) p=p.previousElementSibling;
                e=p;
                keyMap.stop(event);
                break;
       case "=":   //当前节点展开
           if(!e.box) return false;
           if(e.previousElementSibling.className.match(/closed/))  e.previousElementSibling.click();
           Each.call(e.box.querySelectorAll("i.closed"),(ee)=>{
               ee.click();
           })
           return keyMap.stop(event);
       case "-":   //当前节点全部折叠
           if(!e.box) return false;
           if(!e.previousElementSibling.className.match(/closed/))  e.previousElementSibling.click();
           Each.call(e.box.querySelectorAll("i"),(ee)=>{
               if(!ee.className.match(/closed/))  ee.click();
           })
           return keyMap.stop(event);
       case "ctrl+alt+=":   //全部节点展开
           Each.call(document.querySelectorAll("i.closed"),(ee)=>{
               ee.click();
           })
           return keyMap.stop(event);
       case "ctrl+alt+-":   //全部节点全部折叠
           Each.call(document.querySelectorAll("i"),(ee)=>{
               if(!ee.className.match(/closed/))  ee.click();
           })
           document.querySelector("i").click();
           return keyMap.stop(event);
       case "c":
            keyMap.copy( e.innerText.substr(1,e.innerText.length-1));
            break;
       case "v":
            keyMap.copy(e.nextElementSibling.innerText);
            break;
       case "y":        //复制
           keyMap.copy(e.box? e.box.previousElementSibling.innerText.trim()+e.box.innerText.replace(/\s+/g,'')+e.box.nextElementSibling.innerText.trim(): e.innerText+" : "+e.nextElementSibling.innerText);
           break;
       default: 
            return true;
   }
   if(e) {  
       if(old_e) old_e.className=old_e.className.replace(/ active/  ,'');
        e.className+=" active";
        keyMap.e=e;
    }
})